#define _CRT_SECURE_NO_WARNINGS 1
#include"seqlist.h"
#include<stdio.h>

//顺序表的初始化
void SLInit(SL* ps)
{
	//断言
	assert(ps);

	//malloc空间给a
	ps->a = (SLDataType*)malloc(sizeof(SLDataType) * 4);
	if (ps->a == NULL)
	{
		perror("malloc failed");
		exit(-1);
	}
	//对其余变量进行正确的赋值
	ps->size = 0;
	ps->capacity = 4;
}

//顺序表的销毁
void SLDestory(SL* ps)
{
	//断言
	assert(ps);
	//释放掉动态开辟的空间
	free(ps->a);
	ps->a = NULL;
	//其余变量赋为0
	ps->capacity = 0;
	ps->size = 0;
}

//顺序表的打印
void SLPrintf(SL* ps)
{
	//断言
	assert(ps);

	//循环打印
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}


//顺序表容量的检查
void SLCheckCapacity(SL* ps)
{
	//断言
	assert(ps);
	//判断是否容量已满
	if (ps->size == ps->capacity)
	{
		ps->a = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * (ps->capacity * 2));

		if (ps->a == NULL)
		{
			printf("relloc failed");
			exit(-1);
		}
	}
	//修改容量大小
	ps->capacity *= 2;
}

//查找顺序表某值
void SLFind(SL* ps, SLDataType x)
{
	//断言
	assert(ps);
	//循环查找，找到返回下标，否则返回-1
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}
//顺序表的头插
void SLPushFront(SL* ps, SLDataType x)
{
	////断言
	assert(ps);
	////先检查容量
	//SLCheckCapacity(ps);
	////进行挪动。数据全部往后挪，从后往前依次挪，
	//int end = ps->size - 1;
	//while (end >= 0)
	//{
	//	ps->a[end + 1] = ps->a[end];
	//	end--;
	//}
	//ps->a[0] = x;
	//ps->size++;
	///*int i = 0;
	//for (i = ps->size; i >0; i--)
	//{
	//	ps->a[i - 1] = ps->a[i];
	//}
	//ps->a[0] = x;
	//ps->size++;*/
	SLInsert(ps, 0, x);

}
//顺序表的头删
void SLPopFront(SL* ps)
{
	////断言
	assert(ps);
	////进行挪动。数据全部往前挪，从前往后依次挪，
	////暴力检查越界
	//assert(ps->size > 0);
	//int begin = 1;
	//while (begin < ps->size)
	//{
	//	ps->a[begin - 1] = ps->a[begin];
	//	begin++;
	//}
	////size--
	//ps->size--;
	SLErase(ps, 0);
}

//顺序表的尾插
void SLPushBack(SL* ps, SLDataType x)
{
	//断言
	assert(ps);
	////先检查容量
	//SLCheckCapacity(ps);
	////赋值
	//ps->a[ps->size] = x;
	//ps->size++;
	SLInsert(ps, ps->size, x);
}

//顺序表的尾删
void SLPopBack(SL* ps)
{
	//断言
	assert(ps);
	//暴力检查越界
	assert(ps->size > 0);
	ps->size--;
}

//在顺序表的下标Pos处前插入x
void SLInsert(SL* ps, int Pos, SLDataType x)
{
	//断言
	assert(ps);
	assert(Pos >= 0 && Pos <= ps->size);
	//检查容量
	SLCheckCapacity(ps);
	//将Pos及Pos以后得整体向后移，其中是从后往前移
	int end = ps->size - 1;
	while (end >= Pos)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	//赋值
	ps->a[Pos] = x;
	ps->size++;
}

//删除在顺序表中下标Pos处的值
void SLErase(SL* ps, int Pos)
{

	//断言
	assert(ps);
	assert(Pos >= 0 && Pos < ps->size);

	//将Pos及Pos以后得整体向后移，其中是从后往前移
	int begin = Pos + 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		begin++;
	}
	//赋值
	ps->size--;

}
void SLModify(SL* ps, int pos, SLDataType x)
{
	//断言
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	//修改
	ps->a[pos] = x;
}







